package com.example.h3x.androidmalware.View.Fragments;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.h3x.androidmalware.View.Dialogs.HardwareDialog;
import com.example.h3x.androidmalware.R;


public class HardwareFragment extends Fragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    /**
     * simple method taken from
     * https://github.com/h3xb0y/AndroidTaker
     */

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_hardware, container, false);
        TelephonyManager manager = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);

        final String imei = ((TelephonyManager) getContext().getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
        ((TextView) v.findViewById(R.id.imei)).setText(imei);
        WifiManager wifi = (WifiManager) getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        try {
            ((TextView) v.findViewById(R.id.operator)).setText(manager.getNetworkOperatorName());
            WifiInfo wifiInf = wifi.getConnectionInfo();
            int ipAddress = wifiInf.getIpAddress();
            //getting ip
            String ip = String.format("%d.%d.%d.%d", (ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));
            ((TextView) v.findViewById(R.id.ip)).setText(ip);
            //android version
            ((TextView) v.findViewById(R.id.version)).setText(android.os.Build.VERSION.RELEASE);
            //sim operator
            //sim serial
            ((TextView) v.findViewById(R.id.sim)).setText(manager.getSimSerialNumber());

        } catch (NullPointerException e){}

        if (checkRootMethod1())
            ((TextView) v.findViewById(R.id.root)).setText("true");
        else
            ((TextView) v.findViewById(R.id.root)).setText("false");

        Button btnHardware = (Button)   v.findViewById(R.id.HardwareButton);

        View.OnClickListener oclBtnOk = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HardwareDialog dialog = new HardwareDialog();
                dialog.show(getFragmentManager(), "HARDWARE");            }
        };
        btnHardware.setOnClickListener(oclBtnOk);

        return v;
    }


    private static boolean checkRootMethod1() {
        String buildTags = android.os.Build.TAGS;
        return buildTags != null && buildTags.contains("test-keys");
    }
}
